﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEELab
{
    public partial class EditFunction : System.Web.UI.Page
    {
        /// <summary>
        /// Occures on every page load (first time or Post Back).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null && BEELab.Logic.ProgramManagement.FunctionManagement.GetOwnerID(int.Parse(Request.QueryString["ID"].ToString())) != int.Parse(Session["UserID"].ToString()) && (Session["Admin"] == null || (bool)Session["Admin"] == false))
            {
                Response.Redirect("Main.aspx");
            }

            if (!IsPostBack && Request.QueryString["ID"] != null)
            {
                txtName.Text = BEELab.Logic.ProgramManagement.FunctionManagement.GetName(int.Parse(Request.QueryString["ID"].ToString()));
                txtDescription.Text = BEELab.Logic.ProgramManagement.FunctionManagement.GetDescription(int.Parse(Request.QueryString["ID"].ToString()));   
            }
            else
            {
                literal1.Text = "<script>parent.$.fancybox.close(); window.parent.location.reload();    </script>";
            }
        }

        /// <summary>
        /// Occuers once clicking on "submit" button in order to update function details.
        /// Will update function Name, Description, Owner and Visiblity on DB
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            BEELab.Logic.ProgramManagement.FunctionManagement.UpdateFunction(int.Parse(Request.QueryString["ID"].ToString()), txtName.Text, txtDescription.Text, int.Parse(Session["UserID"].ToString()), true);
        }
    }
}